<template>
  <main class="indent">
    <section class="indent-con">
      <h4>我的订单</h4>
      <ul class="con-list">
        <li v-for="item in indentList" :key="item.id">
          <div class="list-site">
            <h4>收获信息:</h4>
            <ul>
              <li>姓名：{{ item.name }}</li>
              <li>电话：{{ item.phone }}</li>
              <li>
                收获地址：
                {{ item.address }}
              </li>
              <!-- <li>下单时间：2021年11月11日 11:11:11</li> -->
              <li>下单时间：{{ fromatDate(item.time) }}</li>
            </ul>
          </div>
          <div
            class="list-item"
            v-for="goods in item.goodsList"
            :key="goods.id"
          >
            <div>
              <h4>{{ goods.name }}</h4>
              <p>￥{{ goods.price }}</p>
            </div>
            <span>X{{ goods.amount }}</span>
          </div>

          <div class="list-bottom">
            <span>付款：{{ item.total }}元</span>
            <span>状态：{{ state(item.state) }}</span>
            <button
              :class="item.state == 2 ? 'receipted' : 'unreceipted'"
              @click="upState(item.id, item.state)"
            >
              确认收货
            </button>
          </div>
        </li>
      </ul>
    </section>
  </main>
</template>

<script>
import { getOrderApi, upStateApi } from "../../api";
import moment from "moment";
export default {
  name: "Indent",
  data() {
    return {
      indentList: [],
    };
  },
  methods: {
    state(code) {
      if (code == 0) {
        return "已付款";
      } else if (code == 1) {
        return "以发货";
      } else {
        return "已收货";
      }
    },
    getOrder() {
      getOrderApi(
        (response) => {
          if (response.data.status === 1) {
            this.indentList = response.data.data;
          }
        },
        (err) => {
          console.log(err);
        }
      );
    },
    upState(id, state) {
      if (state === 2) return;
      upStateApi(
        (response) => {
          if (response.data.status === 1) {
            this.getOrder();
          }
        },
        (err) => {
          console.log(err);
        },
        { state: 2, id }
      );
    },
    fromatDate(time) {
      return moment(time).format("YYYY年MM月DD日 HH:mm:ss");
    },
  },
  mounted() {
    this.getOrder();
  },
};
</script>

<style lang="stylus" scoped>
.indent {
  width: 100%;
  margin-top: 60px;
  min-height: 400px;
  min-width: 800px;

  // background-color: #f8f8f8;
  .indent-con {
    width: 80%;
    min-height: 400px;
    min-width: 800px;
    margin: 0 auto;
    padding: 20px 0 30px;

    &>h4 {
      padding-bottom: 20px;
      font-size: 26px;
      line-height: 50px;
      color: #fe4d1b;
    }
  }

  .con-list {
    &>li {
      padding: 20px;
      background-color: #f8f8f8;
      box-shadow: 0 0 6px 1px rgba(0, 0, 0, 0.3);
      margin-bottom: 20px;
    }
  }

  .list-site {
    padding: 10px;
    font-size: 12px;
    border-radius: 4px;
    background-color: #fff;
    color: #999;

    h4 {
      color: #000;
      padding-left: 4px;
      border-left: 4px solid #fe4d1b;
    }
  }

  .list-item {
    height: 48px;
    background-color: #fff;
    margin-top: 10px;
    padding: 10px;
    color: #999;
    position: relative;
    border-radius: 4px;

    h4 {
      font-size: 16px;
    }

    p {
      font-size: 14px;
      color: #fe4d1b;
    }

    span {
      font-size: 16px;
      line-height: 68px;
      position: absolute;
      top: 0;
      right: 20px;
    }
  }

  .list-bottom {
    margin-top: 20px;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;

    span:first-of-type {
      color: #fe4d1b;
    }

    button {
      padding: 4px 10px;
      border: none;
      border-radius: 4px;
      color: #fff;
      cursor: pointer;
    }
  }

  .unreceipted {
    background-color: #fe4d1b;
  }

  .receipted {
    background-color: #ffcbbc;
  }
}
</style>